/**
 * @Project Name:temo-service
 * @Package Name:cn.hoofungson.temo.service.controller
 * @Since JDK 1.8
 */
package cn.hoofungson.temo.service.controller;

import cn.hoofungson.temo.service.model.todo.User;
import io.swagger.annotations.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import static java.util.Collections.*;

/**
 * @Description: 用户控制器
 * @Author 胡松 hoofungson@163.com
 * @Date 2017-11-28 16:58
 * @Version V1.0
 */
@RestController
@RequestMapping(value = "/api/user")
@Api(description = "用户相关Api")
public class UserController {

    private final Log log = LogFactory.getLog(UserController.class);

    static Map<String, User> userMap = synchronizedMap(new HashMap<String, User>());

    @ApiOperation(value = "获取用户列表", notes = "")
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数没填好"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
    })
    @GetMapping(value = {""})
    public List<User> getUserList() {
        List<User> users = new ArrayList<User>(userMap.values());
        return users;
    }

    @ApiOperation(value = "创建用户", notes = "根据User对象创建用户")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数没填好"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
    })
    @PostMapping(value = {""})
    public String postUser(@RequestBody User user) {
        userMap.put(user.getId(), user);
        return "success";
    }

    @ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", paramType = "path", required = true, dataType = "String")
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数没填好"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
    })
    @GetMapping(value = "/{id}")
    public User getUser(@PathVariable String id) {
        log.info("id = " + id);
        return userMap.get(id);
    }

    @ApiOperation(value = "更新用户详细信息", notes = "根据url的id来指定更新对象，并根据传过来的user信息来更新用户详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", paramType = "path", required = true, dataType = "String"),
            @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    })
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数没填好"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
    })
    @PutMapping(value = "/{id}")
    public String putUser(@PathVariable String id, @RequestBody User user) {
        log.info("id = " + id);
        User user1 = userMap.get(id);
        user1.setName(user.getName());
        userMap.put(id, user1);
        return "success";
    }

    @ApiOperation(value = "删除用户", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "用户ID", paramType = "path", required = true, dataType = "String")
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数没填好"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
    })
    @DeleteMapping(value = "/{id}")
    public String deleteUser(@PathVariable String id) {
        log.info("id = " + id);
        userMap.remove(id);
        return "success";
    }
}
